﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Business;

namespace TicketManagement.Home
{
    public partial class Dashboard : System.Web.UI.Page
    {
        private TicketBus bus;
        protected void Page_Load(object sender, EventArgs e)
        {
            bus = new TicketBus();
            if (!IsPostBack)
            {
                this.loadNewRequest();
                this.loadMyRequest();
            }

        }

        public void loadNewRequest() {
            string orderBy = "order by TKT_NUMBER";
            if (ViewState["sortExpr"] != null)
            {
                orderBy = "order by " + ViewState["sortExpr"] + " " + GetSortDirection();
            }
            listNewRequest.DataSource = bus.getByStatus(1, orderBy);
            listNewRequest.DataBind();
        }

        public void loadMyRequest() {
            string orderBy = "order by TKT_NUMBER";

            int staffId = 0;
            if (Session["User"] != null)
            {
                StaffBus staffBus = new StaffBus();
                Core.Entity.Staff staff = staffBus.getStaffByUserId(Convert.ToInt32(Session["User"]));
                staffId = staff.Id;
            }

            // Get all type
            String statusIds = "";

            if (ViewState["sortExpr"] != null)
            {
                orderBy = "order by " + ViewState["sortExpr"] + " " + GetSortDirection();
            }
            listMyRequest.DataSource = bus.getByStaff(staffId, statusIds, orderBy);
            listMyRequest.DataBind();
        }

        protected void listNewRequest_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            listNewRequest.PageIndex = e.NewPageIndex;
            this.loadNewRequest();
        }

        private string GridViewSortDirection
        {
            get { return ViewState["SortDirection"] as string ?? "DESC"; }
            set { ViewState["SortDirection"] = value; }
        }

        private string GetSortDirection()
        {
            switch (GridViewSortDirection)
            {
                case "ASC": GridViewSortDirection = "DESC"; break;
                case "DESC": GridViewSortDirection = "ASC"; break;
            }
            return GridViewSortDirection;
        }

        protected void listNewRequest_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["sortExpr"] = e.SortExpression;
            this.loadNewRequest();
        }

        protected void listMyRequest_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            listMyRequest.PageIndex = e.NewPageIndex;
            this.loadMyRequest();
        }

        protected void listMyRequest_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["sortExpr"] = e.SortExpression;
            this.loadMyRequest();
        }

    }
}